<!-- commission -->
<template>
  <div class="commission content_box">
    <div class="header_row flex align-center flex-wrap" v-if="hasBtn('commis:query')">
      <div class="flex align-center m-r-2">
        <span>分润方案名称：</span>
        <el-input v-model="searchObj.Name" placeholder="搜索分润方案名称" clearable></el-input>
      </div>

      <div class="flex align-center m-r-2">
        <span>分润时间：</span>
        <el-date-picker v-model="searchObj.timeValue" value-format="yyyy-MM-dd HH:mm:ss" type="datetimerange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
        </el-date-picker>
      </div>

      <!-- 新增的下拉框 -->
      <div class="flex align-center m-r-2">
        <span>方案类型：</span>
        <el-select v-model="value" placeholder="所有方案">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </div>

      <el-button type="primary" @click="handleFilter">查询</el-button>
    </div>

    <div class="m-b-1 neck_box flex align-center space-between">
      <div>
        <el-button v-if="hasBtn('commis:add')" type="success" @click="(currentItem = {}), (showForm = true)">新增分润方案</el-button>
        <!-- <el-button type="danger">批量删除</el-button> -->
        <el-button v-if="hasBtn('commis:export')" type="warning" disabled>导出列表</el-button>
      </div>
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.PageIndex" :max-height="tableHeight" :page-size="listQuery.PageSize" :page-sizes="[10, 20, 50, 100, 200]" layout="total,sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <el-table v-loading="listLoading" :data="list" @sort-change="sortChange" style="width: 100%;" border row-key="Id" :max-height="tableHeight" ref="commisTable" @expand-change="expand" stripe>
      <el-table-column type="expand">
        <template slot-scope="{ row }">
          <!-- 新增 -->
          <div v-if="row.Mains">
            <div class="title_boxs box-header-bg">
              <div class="title_box">阶梯方案</div>
              <div class="title_box">方案金额</div>
              <div class="title_box">销售方案</div>
              <div class="title_box">补贴比例</div>
            </div>
            <div v-for="(item, index) in row.Mains" :key="index" :class=" index%2 == 0 ? '' : 'box-bg' " class="ladder_items">
              <div class="ladder_boxs">{{ item.StairName }}</div>
              <div v-for="(cell, idx) of item.Settings" :key="idx" class="flex_1">

                <div class="ladder_boxs ladder_boxs33">{{ cell.TargetName  +'：'+ cell.TargetNum }} 元</div>
                <div class="ladder_boxs ladder_boxs33">
                  <div class="box" v-for="(ratio, ratioIdx) of cell.Ratios" :key="ratioIdx">
                    {{ ratio.ShareName + '：' + ratio.ShareRation + '%' }}
                  </div>
                </div>
                <div class="ladder_boxs ladder_boxs33">
                  <div class="box" v-for="(ratio, ratioIdx) of cell.Ratios" :key="ratioIdx">
                    {{ ratio.Subsidy + '%' }}
                  </div>
                </div>
              </div>
            </div>
          </div>

          <!-- <div class="ladder_box">
            <div
              v-if="row.Mains"
              class="flex m-t-1 align-center space-between ladder_item "
              v-for="(item, index) in row.Mains"
              :key="index"
            >
              <div class="ladder_name">{{ item.StairName }}</div>
              <div class="flex-1">
                <div
                  class="flex align-center space-between ladder_cell"
                  v-for="(cell, idx) of item.Settings"
                  :key="idx"
                >
                  <span>{{ cell.TargetName }}</span>
                  <span style="margin-left: auto; margin-right: 10%"
                    >{{ cell.TargetNum }}元</span
                  >
                  <div>
                    <p
                      class="flex align-center space-between"
                      v-for="(ratio, ratioIdx) of cell.Ratios"
                      :key="ratioIdx"
                    >
                      <span class="numberSpan">{{ ratio.ShareName }}：</span>
                      <span>{{ ratio.ShareRation }}%</span>

                      旧代码注释部分
                      <el-input
                        type="number"
                        v-model="ratio.ShareRation"
                        :disabled="true"
                      >
                        <template slot="append">%</template>
                      </el-input>
                    ----------------

                    </p>
                  </div>
                </div>
              </div>
            </div>

          </div> -->
        </template>
      </el-table-column>
      <el-table-column label="序号" type="index" align="center">
      </el-table-column>
      <el-table-column prop="SpName" label="分润名称" align="center" sortable>
      </el-table-column>

      <el-table-column prop="SubsidyDate" label="补贴时间（天）" align="center" sortable>
      </el-table-column>

      <el-table-column prop="Remark" label="备注" align="center">
      </el-table-column>
      <el-table-column prop="IsSystem" label="方案类型" align="center" sortable>
        <template slot-scope="scope">
          <div>
            {{
              scope.row.IsSystem == 1
                ? "系统方案"
                : scope.row.IsSystem == 2
                ? "身份方案"
                : scope.row.IsSystem == 3
                ? "公司方案"
                : "机构团队方案"
            }}
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="UserName" label="方案所属" align="center">
      </el-table-column> -->
      <el-table-column prop="CreateBy" label="创建人员" align="center" sortable>
      </el-table-column>
      <el-table-column prop="CreateDate" label="创建时间" width="180" align="center" sortable>
      </el-table-column>
      <el-table-column prop="UpdateBy" label="修改人员" align="center" sortable>
      </el-table-column>
      <el-table-column prop="UpdateDate" label="修改时间" align="center" width="180" sortable>
      </el-table-column>
      <el-table-column prop="IsActive" label="是否有效" align="center" sortable>
        <template slot-scope="scope">
          <el-switch v-model="scope.row.IsActive" @change="valid(scope.row.Id)" :disabled="!hasBtn('commis:active')">
            <!-- :disabled="!hasBtn('commis:active') ? true : !(scope.row.IsSystem == 1)" switch除系统身份不能编辑 -->
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="150" align="center" v-if="hasBtn('commis:edit') || hasBtn('commis:delete')">
        <template slot-scope="scope">
          <!-- :disabled="!(scope.row.IsSystem == 1)" 编辑button除系统身份不能编辑 -->
          <el-button v-if="hasBtn('commis:edit')" type="primary" size="small" @click="(currentItem = $deepClone(scope.row)), (showForm = true)">编辑</el-button>
          <el-button v-if="hasBtn('commis:delete')" @click="$delConfirm(() => del(scope.row.Id))" size="small" type="danger">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- <el-dialog
      :title="dialogTitle"
      class="dialog"
      :visible.sync="dialogVisible"
      :width="'800px'"
      :destroy-on-close="true"
    >
      <dialog-table :dialog-type="dialogType"></dialog-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button @click="dialogVisible = false">保 存</el-button>
      </span>
    </el-dialog> -->
    <CommissionForm :show.sync="showForm" :form-data="currentItem" @finish="getList"></CommissionForm>
  </div>
</template>

<script>
  import { getShare, delShare, toggleValidShare, getShareDetail } from '@/api/system/share-manage'
  import Pagination from '@/components/Pagination' // secondary package based on el-pagination
  import dialogTable from './commis_components/dialog_table.vue'
  import { deepClone } from '@/utils'
  import CommissionForm from './commis_components/CommissionForm'

  export default {
    //import引入的组件需要注入到对象中才能使用
    name: 'commission',
    components: { dialogTable, Pagination, CommissionForm },
    data() {
      //这里存放数据
      return {
        dialogVisible: false,
        tableHeight: 200,
        dialogType: "add",
        showForm: false,
        currentItem: {},
        list: [],
        total: 0,
        listLoading: true,
        searchObjTmpl: {},
        searchObj: {
          Name: '',
          timeValue: '',
          userName: "",
        },
        listQuery: {
          PageIndex: 1,
          PageSize: 10,
          IsPaging: true, //是否分页
          SortName: 'CreateDate',
          SortType: false,
          IsSystem: 0
        },
        currentDetail: {},
        options: [ //下拉框值
          {
            value: 0,
            label: '所有方案'
          },
          {
            value: 2,
            label: '身份方案'
          },
          {
            value: 1,
            label: '系统方案'
          },
          // {
          //   value: 3,
          //   label: '公司方案'
          // },
          {
            value: 4,
            label: '团队方案'
          }
        ],
        value: 0, //下拉框显示的值
      };
    },
    //监听属性 类似于data概念
    computed: {
      dialogTitle() {
        return this.dialogType == 'add' ? '新增分润方案' : '编辑分润方案'
      },
    },
    //监控data中的数据变化
    watch: {},

    //生命周期 - 创建完成（可以访问当前this实例）
    created() {
      this.searchObjTmpl = deepClone(this.searchObj)
      this.getList()
    },
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {
      this.$nextTick(() => {
        this.resizeFun()
      })
      window.addEventListener('resize', this.resizeFun)

    },
    destroyed() {
      window.removeEventListener('resize', this.resizeFun)
    },
    //方法集合
    methods: {
      handleSizeChange(val) {
        this.listQuery.PageSize = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.listQuery.PageIndex = val
        this.getList()
      },
      resizeFun() {
        this.tableHeight = window.innerHeight - this.$refs.commisTable.$el.offsetTop - 150;
      },
      handleClick(index, row) {
        this.handleDialogFun('edit', { index, row })
      },
      handleDelete(index, row) {

      },
      handleDialogFun(type, obj) {
        this.dialogType = type
        this.dialogVisible = true
      },
      resetFilter() {
        this.searchObj = deepClone(this.searchObjTmpl)
        this.handleFilter()
      },
      sortChange(data) {
        const { prop, order } = data
        this.listQuery.SortName = prop
        if (order === 'ascending') {
          this.listQuery.SortType = true
        } else {
          this.listQuery.SortType = false
        }
        this.handleFilter()
      },
      handleFilter() {
        this.listQuery.PageIndex = 1
        this.getList()
      },
      getQuery() {
        const params = {
          ...this.searchObj,
          ...this.listQuery,
        }
        if (params.timeValue && params.timeValue.length) {
          params.StartTime = params.timeValue[0]
          params.EndTime = params.timeValue[1]
        }
        return params
      },
      async getList() {
        this.listLoading = true
        this.listQuery.IsSystem = this.value
        const params = this.getQuery()
        const { Data, Total } = await getShare(params)
        this.listLoading = false
        if (Data) {
          Data.forEach(item => {
            for (let cell in item) {
              if (!item[cell] && this.$getType(item[cell]) !== 'Number') {
                item[cell] = '-'
              }
            }
          })
        }
        this.list = Data || []
        this.total = Total || 0
      },
      async del(id) {
        const { code } = await delShare({
          Id: id
        })
        if (code == 200) {
          this.$notify({
            title: '删除成功',
            message: 'success',
            type: 'success',
            duration: 2000
          })
          this.getList()
        }
      },
      async valid(id) {
        const { code } = await toggleValidShare({
          Id: id
        })
        if (code == 200) {
          this.$notify({
            title: '切换成功',
            message: 'success',
            type: 'success',
            duration: 2000
          })
          this.getList()
        }
      },
      expand(row, expand) {
        if (!row.OpenExpand) {
          row.OpenExpand = true
          this.getDetail(row)
        }

      },
      async getDetail(row) {
        const { data } = await getShareDetail({
          Id: row.Id
        })
        for (let Mains of data.Mains) {
          console.log(Mains)
        }
        console.log(data)
        // row.Mains = data.Mains
        this.$set(row, 'Mains', data.Mains)
      },
    },
  }
</script>
//@import url(); 引入公共css类
<style scoped>
  .ladder_item {
    margin-top: 0;
    width: 85%;
  }
  .ladder_item + .ladder_item {
    border-top: 1px solid #cecece;
  }
  .ladder_cell + .ladder_cell {
    border-top: 2px dashed #f6bcbc;
  }
  .ladder_name {
    width: 33%;
  }
  .ladder_box {
    padding: 0;
    width: 50%;
    min-width: 500px;
    margin: 0 auto;
  }
  .ladder_box .flex > span:first-child {
    white-space: nowrap;
  }
  .ladder_box .flex > .el-input {
    width: 80px;
  }

  .numberSpan {
    flex: 1;
    text-align: right;
  }
  .commission p {
    min-width: 128px;
  }
  .ladder_items {
    width: 100%;
    display: flex;
    border-left: 1px solid #e3e3e3;
  }
  .ladder_boxs {
    width: 25%;
    text-align: center;
    padding: auto 0;
    box-sizing: border-box;
    border-right: 1px solid #e3e3e3;
    border-bottom: 1px solid #e3e3e3;
    -webkit-box-align: center;
    display: flex;
    justify-content: center;
    line-height: 40åpx;
    align-items: center;
    flex-direction: column;
  }
  .ladder_boxs.ladder_boxs33 {
    width: 33.33%;
  }
  .flex_1 {
    display: flex;
    flex: 1;
  }
  .flex_1 :first-child {
    border-right: 0;
  }
  /* .flex_1 > .ladder_boxs{
                                  width: 50%;
                                } */
  .box {
    line-height: 30px;
  }
  .title_boxs {
    width: 100%;
    display: flex;
    border: 1px solid #e3e3e3;
    border-right: 0;
  }
  /* .title_boxs :nth-child(2) {
                                  width: 50%;
                                } */
  .title_box {
    text-align: center;
    width: 25%;
    line-height: 50px;
    border-right: 1px solid #e3e3e3;
  }
</style>
